home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / UTIL / PL 2.0 SupplementDoc Folder.sit / PL 2.0 SupplementDoc Folder / Documentation / Chapter 11. Packages < prev    next >
Text File  |  1995-03-27  |  97KB  |  2,058 lines

  1. Common Lisp the Language, 2nd Edition
  2. -------------------------------------------------------------------------------
  3.  
  4. 11. Packages
  5.  
  6. One problem with earlier Lisp systems is the use of a single name space for all
  7. symbols. In large Lisp systems, with modules written by many different
  8. programmers, accidental name collisions become a serious problem. Common Lisp
  9. addresses this problem through the package system, derived from an earlier
  10. package system developed for Lisp Machine Lisp [55]. In addition to preventing
  11. name-space conflicts, the package system makes the modular structure of large
  12. Lisp systems more explicit.
  13.  
  14. A package is a data structure that establishes a mapping from print names
  15. (strings) to symbols. The package thus replaces the ``oblist'' or ``obarray''
  16. machinery of earlier Lisp systems. At any given time one package is current,
  17. and this package is used by the Lisp reader in translating strings into
  18. symbols. The current package is, by definition, the one that is the value of
  19. the global variable *package*. It is possible to refer to symbols in packages
  20. other than the current one through the use of package qualifiers in the printed
  21. representation of the symbol. For example, foo:bar, when seen by the reader,
  22. refers to the symbol whose name is bar in the package whose name is foo.
  23. (Actually, this is true only if bar is an external symbol of foo, that is, a
  24. symbol that is supposed to be visible outside of foo. A reference to an
  25. internal symbol requires the intentionally clumsier syntax foo::bar.)
  26.  
  27. The string-to-symbol mappings available in a given package are divided into two
  28. classes, external and internal. We refer to the symbols accessible via these
  29. mappings as being external and internal symbols of the package in question,
  30. though really it is the mappings that are different and not the symbols
  31. themselves. Within a given package, a name refers to one symbol or to none; if
  32. it does refer to a symbol, then it is either external or internal in that
  33. package, but not both.
  34.  
  35. External symbols are part of the package's public interface to other packages.
  36. External symbols are supposed to be chosen with some care and are advertised to
  37. users of the package. Internal symbols are for internal use only, and these
  38. symbols are normally hidden from other packages. Most symbols are created as
  39. internal symbols; they become external only if they appear explicitly in an
  40. export command for the package.
  41.  
  42. A symbol may appear in many packages. It will always have the same name
  43. wherever it appears, but it may be external in some packages and internal in
  44. others. On the other hand, the same name (string) may refer to different
  45. symbols in different packages.
  46.  
  47. Normally, a symbol that appears in one or more packages will be owned by one
  48. particular package, called the home package of the symbol; that package is said
  49. to own the symbol. Every symbol has a component called the package cell that
  50. contains a pointer to its home package. A symbol that is owned by some package
  51. is said to be interned. Some symbols are not owned by any package; such a
  52. symbol is said to be uninterned, and its package cell contains nil.
  53.  
  54. Packages may be built up in layers. From the point of view of a package's user,
  55. the package is a single collection of mappings from strings into internal and
  56. external symbols. However, some of these mappings may be established within the
  57. package itself, while other mappings are inherited from other packages via the
  58. use-package construct. (The mechanisms responsible for this inheritance are
  59. described below.) In what follows, we will refer to a symbol as being
  60. accessible in a package if it can be referred to without a package qualifier
  61. when that package is current, regardless of whether the mapping occurs within
  62. that package or via inheritance. We will refer to a symbol as being present in
  63. a package if the mapping is in the package itself and is not inherited from
  64. somewhere else. Thus a symbol present in a package is accessible, but an
  65. accessible symbol is not necessarily present.
  66.  
  67. A symbol is said to be interned in a package if it is accessible in that
  68. package and also is owned (by either that package or some other package).
  69. Normally all the symbols accessible in a package will in fact be owned by some
  70. package, but the terminology is useful when discussing the pathological case of
  71. an accessible but unowned (uninterned) symbol.
  72.  
  73. As a verb, to intern a symbol in a package means to cause the symbol to be
  74. interned in the package if it was not already; this process is performed by the
  75. function intern. If the symbol was previously unowned, then the package it is
  76. being interned in becomes its owner (home package); but if the symbol was
  77. previously owned by another package, that other package continues to own the
  78. symbol.
  79.  
  80. To unintern a symbol from the package means to cause it to be not present in
  81. the package and, additionally, to cause the symbol to be uninterned if the
  82. package was the home package (owner) of the symbol. This process is performed
  83. by the function unintern.
  84.  
  85. -------------------------------------------------------------------------------
  86.  
  87.    *  Consistency Rules
  88.    *  Package Names
  89.    *  Translating Strings to Symbols
  90.    *  Exporting and Importing Symbols
  91.    *  Name Conflicts
  92.    *  Built-in Packages
  93.    *  Package System Functions and Variables
  94.    *  Modules
  95.    *  An Example
  96.  
  97. -------------------------------------------------------------------------------
  98.  
  99. 11.1. Consistency Rules
  100.  
  101. Package-related bugs can be very subtle and confusing: things are not what they
  102. appear to be. The Common Lisp package system is designed with a number of
  103. safety features to prevent most of the common bugs that would otherwise occur
  104. in normal use. This may seem over-protective, but experience with earlier
  105. package systems has shown that such safety features are needed.
  106.  
  107. In dealing with the package system, it is useful to keep in mind the following
  108. consistency rules, which remain in force as long as the value of *package* is
  109. not changed by the user:
  110.  
  111.    *  Read-read consistency: Reading the same print name always results in the
  112.      same (eq) symbol.
  113.  
  114.    *  Print-read consistency: An interned symbol always prints as a sequence of
  115.      characters that, when read back in, yields the same (eq) symbol.
  116.  
  117.    *  Print-print consistency: If two interned symbols are not eq, then their
  118.      printed representations will be different sequences of characters.
  119.  
  120. These consistency rules remain true in spite of any amount of implicit
  121. interning caused by typing in Lisp forms, loading files, etc. This has the
  122. important implication that, as long as the current package is not changed,
  123. results are reproducible regardless of the order of loading files or the exact
  124. history of what symbols were typed in when. The rules can only be violated by
  125. explicit action: changing the value of *package*, forcing some action by
  126. continuing from an error, or calling one of the ``dangerous'' functions
  127. unintern, unexport, shadow, shadowing-import, or unuse-package.
  128.  
  129. -------------------------------------------------------------------------------
  130.  
  131. 11.2. Package Names
  132.  
  133. Each package has a name (a string) and perhaps some nicknames. These are
  134. assigned when the package is created, though they can be changed later. A
  135. package's name should be something long and self-explanatory, like editor;
  136. there might be a nickname that is shorter and easier to type, such as ed.
  137.  
  138. There is a single name space for packages. The function find-package translates
  139. a package name or nickname into the associated package. The function
  140. package-name returns the name of a package. The function package-nicknames
  141. returns a list of all nicknames for a package. The function rename-package
  142. removes a package's current name and nicknames and replaces them with new ones
  143. specified by the user. Package renaming is occasionally useful when, for
  144. development purposes, it is desirable to load two versions of a package into
  145. the same Lisp. One can load the first version, rename it, and then load the
  146. other version, without getting a lot of name conflicts.
  147.  
  148. When the Lisp reader sees a qualified symbol, it handles the package-name part
  149. in the same way as the symbol part with respect to capitalization. Lowercase
  150. characters in the package name are converted to corresponding uppercase
  151. characters unless preceded by the escape character or surrounded by |
  152. characters. The lookup done by the find-package function is case-sensitive,
  153. like that done for symbols. Note that |Foo|:|Bar| refers to a symbol whose name
  154. is Bar in a package whose name is Foo. By contrast, |Foo:Bar| refers to a
  155. seven-character symbol that has a colon in its name (as well as two uppercase
  156. letters and four lowercase letters) and is interned in the current package.
  157. Following the convention used in this book for symbols, we show ordinary
  158. package names using lowercase letters, even though the name string is
  159. internally represented with uppercase letters.
  160.  
  161. Most of the functions that require a package-name argument from the user accept
  162. either a symbol or a string. If a symbol is supplied, its print name will be
  163. used; the print name will already have undergone case-conversion by the usual
  164. rules. If a string is supplied, it must be so capitalized as to match exactly
  165. the string that names the package.
  166.  
  167. [change_begin]
  168. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  169. one may use either a package object or a package name (symbol or string) in any
  170. of the following situations:
  171.  
  172.    *  the :use argument to make-package
  173.  
  174.    *  the first argument to package-use-list, package-used-by-list,
  175.      package-name, package-nicknames, in-package, find-package, rename-package,
  176.      or delete-package,
  177.  
  178.    *  the second argument to intern, find-symbol, unintern, export, unexport,
  179.      import, shadowing-import, or shadow
  180.  
  181.    *  the first argument, or a member of the list that is the first argument,
  182.      to use-package or unuse-package
  183.  
  184.    *  the value of the package given to do-symbols, do-external-symbols, or
  185.      do-all-symbols
  186.  
  187.    *  a member of the package-list given to with-package-iterator
  188.  
  189. Note that the first argument to make-package must still be a package name and
  190. not an actual package; it makes no sense to create an already existing package.
  191. Similarly, package nicknames must always be expressed as package names and not
  192. as package objects. If find-package is given a package object instead of a
  193. name, it simply returns that package.
  194. [change_end]
  195.  
  196. -------------------------------------------------------------------------------
  197.  
  198. 11.3. Translating Strings to Symbols
  199.  
  200. The value of the special variable *package* must always be a package object
  201. (not a name). Whatever package object is currently the value of *package* is
  202. referred to as the current package.
  203.  
  204. When the Lisp reader has, by parsing, obtained a string of characters thought
  205. to name a symbol, that name is looked up in the current package. This lookup
  206. may involve looking in other packages whose external symbols are inherited by
  207. the current package. If the name is found, the corresponding symbol is
  208. returned. If the name is not found (that is, there is no symbol of that name
  209. accessible in the current package), a new symbol is created for it and is
  210. placed in the current package as an internal symbol. Moreover, the current
  211. package becomes the owner (home package) of the symbol, and so the symbol
  212. becomes interned in the current package. If the name is later read again while
  213. this same package is current, the same symbol will then be found and returned.
  214.  
  215. Often it is desirable to refer to an external symbol in some package other than
  216. the current one. This is done through the use of a qualified name, consisting
  217. of a package name, then a colon, then the name of the symbol. This causes the
  218. symbol's name to be looked up in the specified package, rather than in the
  219. current one. For example, editor:buffer refers to the external symbol named
  220. buffer accessible in the package named editor, regardless of whether there is a
  221. symbol named buffer in the current package. If there is no package named
  222. editor, or if no symbol named buffer is accessible in editor, or if buffer is
  223. an internal symbol in editor, the Lisp reader will signal a correctable error
  224. to ask the user for instructions.
  225.  
  226. On rare occasions, a user may need to refer to an internal symbol of some
  227. package other than the current one. It is illegal to do this with the colon
  228. qualifier, since accessing an internal symbol of some other package is usually
  229. a mistake. However, this operation is legal if a doubled colon :: is used as
  230. the separator in place of the usual single colon. If editor::buffer is seen,
  231. the effect is exactly the same as reading buffer with *package* temporarily
  232. rebound to the package whose name is editor. This special-purpose qualifier
  233. should be used with caution.
  234.  
  235. The package named keyword contains all keyword symbols used by the Lisp system
  236. itself and by user-written code. Such symbols must be easily accessible from
  237. any package, and name conflicts are not an issue because these symbols are used
  238. only as labels and never to carry package-specific values or properties.
  239. Because keyword symbols are used so frequently, Common Lisp provides a special
  240. reader syntax for them. Any symbol preceded by a colon but no package name (for
  241. example :foo) is added to (or looked up in) the keyword package as an external
  242. symbol. The keyword package is also treated specially in that whenever a symbol
  243. is added to the keyword package the symbol is always made external; the symbol
  244. is also automatically declared to be a constant (see defconstant) and made to
  245. have itself as its value. This is why every keyword evaluates to itself. As a
  246. matter of style, keywords should always be accessed using the leading-colon
  247. convention; the user should never import or inherit keywords into any other
  248. package. It is an error to try to apply use-package to the keyword package.
  249.  
  250. Each symbol contains a package cell that is used to record the home package of
  251. the symbol, or nil if the symbol is uninterned. This cell may be accessed by
  252. using the function symbol-package. When an interned symbol is printed, if it is
  253. a symbol in the keyword package, then it is printed with a preceding colon;
  254. otherwise, if it is accessible (directly or by inheritance) in the current
  255. package, it is printed without any qualification; otherwise, it is printed with
  256. the name of the home package as the qualifier, using : as the separator if the
  257. symbol is external and :: if not.
  258.  
  259. A symbol whose package slot contains nil (that is, has no home package) is
  260. printed preceded by #:. It is possible, by the use of import and unintern, to
  261. create a symbol that has no recorded home package but that in fact is
  262. accessible in some package. The system does not check for this pathological
  263. case, and such symbols will always be printed preceded by #:.
  264.  
  265. In summary, the following four uses of symbol qualifier syntax are defined.
  266.  
  267. foo:bar
  268.      When read, looks up BAR among the external symbols of the package named
  269.      FOO. Printed when symbol bar is external in its home package foo and is
  270.      not accessible in the current package.
  271.  
  272. foo::bar
  273.      When read, interns BAR as if FOO were the current package. Printed when
  274.      symbol bar is internal in its home package foo and is not accessible in
  275.      the current package.
  276.  
  277. :bar
  278.      When read, interns BAR as an external symbol in the keyword package and
  279.      makes it evaluate to itself. Printed when the home package of symbol bar
  280.      is keyword.
  281.  
  282. #:bar
  283.      When read, creates a new uninterned symbol named BAR. Printed when the
  284.      symbol bar is uninterned (has no home package), even in the pathological
  285.      case that bar is uninterned but nevertheless somehow accessible in the
  286.      current package.
  287.  
  288. All other uses of colons within names of symbols are not defined by Common Lisp
  289. but are reserved for implementation-dependent use; this includes names that end
  290. in a colon, contain two or more colons, or consist of just a colon.
  291.  
  292. -------------------------------------------------------------------------------
  293.  
  294. 11.4. Exporting and Importing Symbols
  295.  
  296. Symbols from one package may be made accessible in another package in two ways.
  297.  
  298. First, any individual symbol may be added to a package by use of the function
  299. import. The form (import 'editor:buffer) takes the external symbol named buffer
  300. in the editor package (this symbol was located when the form was read by the
  301. Lisp reader) and adds it to the current package as an internal symbol. The
  302. symbol is then present in the current package. The imported symbol is not
  303. automatically exported from the current package, but if it is already present
  304. and external, then the fact that it is external is not changed. After the call
  305. to import it is possible to refer to buffer in the importing package without
  306. any qualifier. The status of buffer in the package named editor is unchanged,
  307. and editor remains the home package for this symbol. Once imported, a symbol is
  308. present in the importing package and can be removed only by calling unintern.
  309.  
  310. If the symbol is already present in the importing package, import has no
  311. effect. If a distinct symbol with the name buffer is accessible in the
  312. importing package (directly or by inheritance), then a correctable error is
  313. signaled, as described in section 11.5, because import avoids letting one
  314. symbol shadow another.
  315.  
  316. A symbol is said to be shadowed by another symbol in some package if the first
  317. symbol would be accessible by inheritance if not for the presence of the second
  318. symbol. To import a symbol without the possibility of getting an error because
  319. of shadowing, use the function shadowing-import. This inserts the symbol into
  320. the specified package as an internal symbol, regardless of whether another
  321. symbol of the same name will be shadowed by this action. If a different symbol
  322. of the same name is already present in the package, that symbol will first be
  323. uninterned from the package (see unintern). The new symbol is added to the
  324. package's shadowing-symbols list. shadowing-import should be used with caution.
  325. It changes the state of the package system in such a way that the consistency
  326. rules do not hold across the change.
  327.  
  328. The second mechanism is provided by the function use-package. This causes a
  329. package to inherit all of the external symbols of some other package. These
  330. symbols become accessible as internal symbols of the using package. That is,
  331. they can be referred to without a qualifier while this package is current, but
  332. they are not passed along to any other package that uses this package. Note
  333. that use-package, unlike import, does not cause any new symbols to be present
  334. in the current package but only makes them accessible by inheritance.
  335. use-package checks carefully for name conflicts between the newly imported
  336. symbols and those already accessible in the importing package. This is
  337. described in detail in section 11.5.
  338.  
  339. Typically a user, working by default in the user package, will load a number of
  340. packages into Lisp to provide an augmented working environment, and then call
  341. use-package on each of these packages to allow easy access to their external
  342. symbols. unuse-package undoes the effects of a previous use-package. The
  343. external symbols of the used package are no longer inherited. However, any
  344. symbols that have been imported into the using package continue to be present
  345. in that package.
  346.  
  347. There is no way to inherit the internal symbols of another package; to refer to
  348. an internal symbol, the user must either make that symbol's home package
  349. current, use a qualifier, or import that symbol into the current package.
  350.  
  351. [change_begin]
  352. The distinction between external and internal symbols is a primary means of
  353. hiding names so that one program does not tread on the namespace of another.
  354. [change_end]
  355.  
  356. When intern or some other function wants to look up a symbol in a given
  357. package, it first looks for the symbol among the external and internal symbols
  358. of the package itself; then it looks through the external symbols of the used
  359. packages in some unspecified order. The order does not matter; according to the
  360. rules for handling name conflicts (see below), if conflicting symbols appear in
  361. two or more packages inherited by package X, a symbol of this name must also
  362. appear in X itself as a shadowing symbol. Of course, implementations are free
  363. to choose other, more efficient ways of implementing this search, as long as
  364. the user-visible behavior is equivalent to what is described here.
  365.  
  366. The function export takes a symbol that is accessible in some specified package
  367. (directly or by inheritance) and makes it an external symbol of that package.
  368. If the symbol is already accessible as an external symbol in the package,
  369. export has no effect. If the symbol is directly present in the package as an
  370. internal symbol, it is simply changed to external status. If it is accessible
  371. as an internal symbol via use-package, the symbol is first imported into the
  372. package, then exported. (The symbol is then present in the specified package
  373. whether or not the package continues to use the package through which the
  374. symbol was originally inherited.) If the symbol is not accessible at all in the
  375. specified package, a correctable error is signaled that, upon continuing, asks
  376. the user whether the symbol should be imported.
  377.  
  378. The function unexport is provided mainly as a way to undo erroneous calls to
  379. export. It works only on symbols directly present in the current package,
  380. switching them back to internal status. If unexport is given a symbol already
  381. accessible as an internal symbol in the current package, it does nothing; if it
  382. is given a symbol not accessible in the package at all, it signals an error.
  383.  
  384. -------------------------------------------------------------------------------
  385.  
  386. 11.5. Name Conflicts
  387.  
  388. A fundamental invariant of the package system is that within one package any
  389. particular name can refer to at most one symbol. A name conflict is said to
  390. occur when there is more than one candidate symbol and it is not obvious which
  391. one to choose. If the system does not always choose the same way, the read-read
  392. consistency rule would be violated. For example, some programs or data might
  393. have been read in under a certain mapping of the name to a symbol. If the
  394. mapping changes to a different symbol, and subsequently additional programs or
  395. data are read, then the two programs will not access the same symbol even
  396. though they use the same name. Even if the system did always choose the same
  397. way, a name conflict is likely to result in a mapping from names to symbols
  398. different from what was expected by the user, causing programs to execute
  399. incorrectly. Therefore, any time a name conflict is about to occur, an error is
  400. signaled. The user may continue from the error and tell the package system how
  401. to resolve the conflict.
  402.  
  403. It may be that the same symbol is accessible to a package through more than one
  404. path. For example, the symbol might be an external symbol of more than one used
  405. package, or the symbol might be directly present in a package and also
  406. inherited from another package. In such cases there is no name conflict. The
  407. same identical symbol cannot conflict with itself. Name conflicts occur only
  408. between distinct symbols with the same name.
  409.  
  410. The creator of a package can tell the system in advance how to resolve a name
  411. conflict through the use of shadowing. Every package has a list of shadowing
  412. symbols. A shadowing symbol takes precedence over any other symbol of the same
  413. name that would otherwise be accessible to the package. A name conflict
  414. involving a shadowing symbol is always resolved in favor of the shadowing
  415. symbol, without signaling an error (except for one instance involving import
  416. described below). The functions shadow and shadowing-import may be used to
  417. declare shadowing symbols.
  418.  
  419. Name conflicts are detected when they become possible, that is, when the
  420. package structure is altered. There is no need to check for name conflicts
  421. during every name lookup.
  422.  
  423. The functions use-package, import, and export check for name conflicts.
  424. use-package makes the external symbols of the package being used accessible to
  425. the using package; each of these symbols is checked for name conflicts with the
  426. symbols already accessible. import adds a single symbol to the internals of a
  427. package, checking for a name conflict with an existing symbol either present in
  428. the package or accessible to it. import signals a name conflict error even if
  429. the conflict is with a shadowing symbol, the rationale being that the user has
  430. given two explicit and inconsistent directives. export makes a single symbol
  431. accessible to all the packages that use the package from which the symbol is
  432. exported. All of these packages are checked for name conflicts: (export s p)
  433. does (find-symbol (symbol-name s) q) for each package q in
  434. (package-used-by-list p). Note that in the usual case of an export during the
  435. initial definition of a package, the result of package-used-by-list will be nil
  436. and the name-conflict checking will take negligible time.
  437.  
  438. The function intern, which is the one used most frequently by the Lisp reader
  439. for looking up names of symbols, does not need to do any name-conflict
  440. checking, because it never creates a new symbol if there is already an
  441. accessible symbol with the name given.
  442.  
  443. shadow and shadowing-import never signal a name-conflict error because the
  444. user, by calling these functions, has specified how any possible conflict is to
  445. be resolved. shadow does name-conflict checking to the extent that it checks
  446. whether a distinct existing symbol with the specified name is accessible and,
  447. if so, whether it is directly present in the package or inherited. In the
  448. latter case, a new symbol is created to shadow it. shadowing-import does
  449. name-conflict checking to the extent that it checks whether a distinct existing
  450. symbol with the same name is accessible; if so, it is shadowed by the new
  451. symbol, which implies that it must be uninterned if it was directly present in
  452. the package.
  453.  
  454. unuse-package, unexport, and unintern (when the symbol being uninterned is not
  455. a shadowing symbol) do not need to do any name-conflict checking because they
  456. only remove symbols from a package; they do not make any new symbols
  457. accessible.
  458.  
  459. Giving a shadowing symbol to unintern can uncover a name conflict that had
  460. previously been resolved by the shadowing. If package A uses packages B and C,
  461. A contains a shadowing symbol x, and B and C each contain external symbols
  462. named x, then removing the shadowing symbol x from A will reveal a name
  463. conflict between b:x and c:x if those two symbols are distinct. In this case
  464. unintern will signal an error.
  465.  
  466. Aborting from a name-conflict error leaves the original symbol accessible.
  467. Package functions always signal name-conflict errors before making any change
  468. to the package structure. When multiple changes are to be made, however, for
  469. example when export is given a list of symbols, it is permissible for the
  470. implementation to process each change separately, so that aborting from a name
  471. conflict caused by the second symbol in the list will not unexport the first
  472. symbol in the list. However, aborting from a name-conflict error caused by
  473. export of a single symbol will not leave that symbol accessible to some
  474. packages and inaccessible to others; with respect to each symbol processed,
  475. export behaves as if it were an atomic operation.
  476.  
  477. Continuing from a name-conflict error should offer the user a chance to resolve
  478. the name conflict in favor of either of the candidates. The package structure
  479. should be altered to reflect the resolution of the name conflict, via
  480. shadowing-import, unintern, or unexport.
  481.  
  482. A name conflict in use-package between a symbol directly present in the using
  483. package and an external symbol of the used package may be resolved in favor of
  484. the first symbol by making it a shadowing symbol, or in favor of the second
  485. symbol by uninterning the first symbol from the using package. The latter
  486. resolution is dangerous if the symbol to be uninterned is an external symbol of
  487. the using package, since it will cease to be an external symbol.
  488.  
  489. A name conflict in use-package between two external symbols inherited by the
  490. using package from other packages may be resolved in favor of either symbol by
  491. importing it into the using package and making it a shadowing symbol.
  492.  
  493. A name conflict in export between the symbol being exported and a symbol
  494. already present in a package that would inherit the newly exported symbol may
  495. be resolved in favor of the exported symbol by uninterning the other one, or in
  496. favor of the already-present symbol by making it a shadowing symbol.
  497.  
  498. A name conflict in export or unintern due to a package inheriting two distinct
  499. symbols with the same name from two other packages may be resolved in favor of
  500. either symbol by importing it into the using package and making it a shadowing
  501. symbol, just as with use-package.
  502.  
  503. A name conflict in import between the symbol being imported and a symbol
  504. inherited from some other package may be resolved in favor of the symbol being
  505. imported by making it a shadowing symbol, or in favor of the symbol already
  506. accessible by not doing the import. A name conflict in import with a symbol
  507. already present in the package may be resolved by uninterning that symbol, or
  508. by not doing the import.
  509.  
  510. Good user-interface style dictates that use-package and export, which can cause
  511. many name conflicts simultaneously, first check for all of the name conflicts
  512. before presenting any of them to the user. The user may then choose to resolve
  513. all of them wholesale or to resolve each of them individually, the latter
  514. requiring a lot of interaction but permitting different conflicts to be
  515. resolved different ways.
  516.  
  517. Implementations may offer other ways of resolving name conflicts. For instance,
  518. if the symbols that conflict are not being used as objects but only as names
  519. for functions, it may be possible to ``merge'' the two symbols by putting the
  520. function definition onto both symbols. References to either symbol for purposes
  521. of calling a function would be equivalent. A similar merging operation can be
  522. done for variable values and for things stored on the property list. In Lisp
  523. Machine Lisp, for example, one can also forward the value, function, and
  524. property cells so that future changes to either symbol will propagate to the
  525. other one. Some other implementations are able to do this with value cells but
  526. not with property lists. Only the user can know whether this way of resolving a
  527. name conflict is adequate, because it will work only if the use of two non-eq
  528. symbols with the same name will not prevent the correct operation of the
  529. program. The value of offering symbol merging as a way of resolving name
  530. conflicts is that it can avoid the need to throw away the whole Lisp world,
  531. correct the package-definition forms that caused the error, and start over from
  532. scratch.
  533.  
  534. -------------------------------------------------------------------------------
  535.  
  536. 11.6. Built-in Packages
  537.  
  538. [old_change_begin]
  539. The following packages, at least, are built into every Common Lisp system.
  540.  
  541. lisp
  542.      The package named lisp contains the primitives of the Common Lisp system.
  543.      Its external symbols include all of the user-visible functions and global
  544.      variables that are present in the Common Lisp system, such as car, cdr,
  545.      and *package*. Almost all other packages will want to use lisp so that
  546.      these symbols will be accessible without qualification.
  547.  
  548. user
  549.      The user package is, by default, the current package at the time a Common
  550.      Lisp system starts up. This package uses the lisp package.
  551.  
  552. [old_change_end]
  553.  
  554. [change_begin]
  555. X3J13 voted in March 1989 (LISP-PACKAGE-NAME)   to specify that the forthcoming
  556. ANSI Common Lisp will use the package name common-lisp instead of lisp and the
  557. package name common-lisp-user instead of user. The purpose is to allow a single
  558. Lisp system to support both ``old'' Common Lisp and ``new'' ANSI Common Lisp
  559. simultaneously despite the fact that in some cases the two languages use the
  560. same names for incompatible purposes. (That's what packages are for!)
  561.  
  562. common-lisp
  563.      The package named common-lisp contains the primitives of the ANSI Common
  564.      Lisp system (as opposed to a Common Lisp system based on the 1984
  565.      specification). Its external symbols include all of the user-visible
  566.      functions and global variables that are present in the ANSI Common Lisp
  567.      system, such as car, cdr, and *package*. Note, however, that the home
  568.      package of such symbols is not necessarily the common-lisp package (this
  569.      makes it easier for symbols such as t and lambda to be shared between the
  570.      common-lisp package and another package, possibly one named lisp). Almost
  571.      all other packages ought to use common-lisp so that these symbols will be
  572.      accessible without qualification. This package has the nickname cl.
  573.  
  574. common-lisp-user
  575.      The common-lisp-user package is, by default, the current package at the
  576.      time an ANSI Common Lisp system starts up. This package uses the
  577.      common-lisp package and has the nickname cl-user. It may contain other
  578.      implementation-dependent symbols and may use other implementation-specific
  579.      packages.
  580.  
  581. [change_end]
  582.  
  583. keyword
  584.      This package contains all of the keywords used by built-in or user-defined
  585.      Lisp functions. Printed symbol representations that start with a colon are
  586.      interpreted as referring to symbols in this package, which are always
  587.      external symbols. All symbols in this package are treated as constants
  588.      that evaluate to themselves, so that the user can type :foo instead of
  589.      ':foo.
  590.  
  591. [old_change_begin]
  592.  
  593. system
  594.      This package name is reserved to the implementation. Normally this is used
  595.      to contain names of implementation-dependent system-interface functions.
  596.      This package uses lisp and has the nickname sys.
  597.  
  598. [old_change_end]
  599.  
  600. [change_begin]
  601. X3J13 voted in January 1989 (PACKAGE-CLUTTER)   to modify the requirements on
  602. the built-in packages so as to limit what may appear in the common-lisp package
  603. and to lift the requirement that every implementation have a package named
  604. system. The details are as follows.
  605.  
  606. Not only must the common-lisp package in any given implementation contain all
  607. the external symbols prescribed by the standard; the common-lisp package
  608. moreover may not contain any external symbol that is not prescribed by the
  609. standard. However, the common-lisp package may contain additional internal
  610. symbols, depending on the implementation.
  611.  
  612. An external symbol of the common-lisp package may not have a function, macro,
  613. or special form definition, or a top-level value, or a special proclamation, or
  614. a type definition, unless specifically permitted by the standard. Programmers
  615. may validly rely on this fact; for example, fboundp is guaranteed to be false
  616. for all external symbols of the common-lisp package except those explicitly
  617. specified in the standard to name functions, macros, and special forms.
  618. Similarly, boundp will be false of all such external symbols except those
  619. documented to be variables or constants.
  620.  
  621. Portable programs may use external symbols in the common-lisp package that are
  622. not documented to be constants or variables as names of local lexical variables
  623. with the presumption that the implementation has not proclaimed such variables
  624. to be special; this legitimizes the common practice of using such names as list
  625. and string as names for local variables.
  626.  
  627. A valid implementation may initially have properties on any symbol, or
  628. dynamically put new properties on symbols (even user-created symbols), as long
  629. as no property indicator used for this purpose is an external symbol of any
  630. package defined by the standard or a symbol that is accessible from the
  631. common-lisp-user package or any package defined by the user.
  632.  
  633. This vote eliminates the requirement that every implementation have a
  634. predefined package named system. An implementation may provide any number of
  635. predefined packages; these should be described in the documentation for that
  636. implementation.
  637.  
  638. The common-lisp-user package may contain symbols not described by the standard
  639. and may use other implementation-specific packages.
  640.  
  641. X3J13 voted in March 1989 (LISP-SYMBOL-REDEFINITION)   to restrict user
  642. programs from performing certain actions that might interfere with built-in
  643. facilities or interact badly with them. Except where explicitly allowed, the
  644. consequences are undefined if any of the following actions are performed on a
  645. symbol in the common-lisp package.
  646.  
  647.    *  binding or altering its value (lexically or dynamically)
  648.    *  defining or binding it as a function
  649.    *  defining or binding it as a macro
  650.    *  defining it as a type specifier (defstruct, defclass, deftype)
  651.    *  defining it as a structure (defstruct)
  652.    *  defining it as a declaration
  653.    *  dsing it as a symbol macro
  654.    *  altering its print name
  655.    *  altering its package
  656.    *  tracing it
  657.    *  declaring or proclaiming it special or lexical
  658.    *  declaring or proclaiming its type or ftype
  659.    *  removing it from the package common-lisp
  660.  
  661. X3J13 also voted in June 1989 (DEFINE-COMPILER-MACRO)   to add to this list the
  662. item
  663.  
  664.    *  defining it as a compiler macro
  665.  
  666. If such a symbol is not globally defined as a variable or a constant, a user
  667. program is allowed to lexically bind it and declare the type of that binding.
  668.  
  669. If such a symbol is not defined as a function, macro, or special form, a user
  670. program is allowed to (lexically) bind it as a function and to declare the
  671. ftype of that binding and to trace that binding.
  672.  
  673. If such a symbol is not defined as a function, macro, or special form, a user
  674. program is allowed to (lexically) bind it as a macro.
  675.  
  676. As an example, the behavior of the code fragment
  677.  
  678. (flet ((open (filename &key direction)
  679.          (format t "~%OPEN was called.")
  680.          (open filename :direction direction)))
  681.   (with-open-file (x "frob" :direction ':output)
  682.     (format t "~%Was OPEN called?")))
  683.  
  684. is undefined. Even in a ``reasonable'' implementation, for example, the macro
  685. expansion of with-open-file might refer to the open function and might not.
  686. However, the preceding rules eliminate the burden of deciding whether an
  687. implementation is reasonable. The code fragment violates the rules; officially
  688. its behavior is therefore completely undefined, and that's that.
  689.  
  690. Note that ``altering the property list'' is not in the list of proscribed
  691. actions, so a user program is permitted to add properties to or remove
  692. properties from symbols in the common-lisp package.
  693. [change_end]
  694.  
  695. -------------------------------------------------------------------------------
  696.  
  697. 11.7. Package System Functions and Variables
  698.  
  699. Some of the functions and variables in this section are described in previous
  700. sections but are included here for completeness.
  701.  
  702. [old_change_begin]
  703. It is up to each implementation's compiler to ensure that when a compiled file
  704. is loaded, all of the symbols in the file end up in the same packages that they
  705. would occupy if the Lisp source file were loaded. In most compilers, this will
  706. be accomplished by treating certain package operations as though they are
  707. surrounded by (eval-when (compile load eval) ...); see eval-when. These
  708. operations are make-package, in-package, shadow, shadowing-import, export,
  709. unexport, use-package, unuse-package, and import. To guarantee proper
  710. compilation in all Common Lisp implementations, these functions should appear
  711. only at top level within a file. As a matter of style, it is suggested that
  712. each file contain only one package, and that all of the package setup forms
  713. appear near the start of the file. This is discussed in more detail, with
  714. examples, in section 11.9.
  715. [old_change_end]
  716.  
  717. [change_begin]
  718. X3J13 voted in March 1989 (IN-PACKAGE-FUNCTIONALITY)   to cancel the
  719. specifications of the preceding paragraph in order to support a model of file
  720. compilation in which the compiler need never take special note of ordinary
  721. function calls; only special forms and macros are recognized as affecting the
  722. state of the compilation process. As part of this change in-package was changed
  723. to be a macro rather than a function and its functionality was restricted. The
  724. actions of shadow, shadowing-import, use-package, import, intern, and export
  725. for compilation purposes may be accomplished with the new macro defpackage.
  726. [change_end]
  727.  
  728. -------------------------------------------------------------------------------
  729. Implementation note: In the past, some Lisp compilers have read the entire file
  730. into Lisp before processing any of the forms. Other compilers have arranged for
  731. the loader to do all of its intern operations before evaluating any of the
  732. top-level forms. Neither of these techniques will work in a straightforward way
  733. in Common Lisp because of the presence of multiple packages.
  734. -------------------------------------------------------------------------------
  735.  
  736. For the functions described here, all optional arguments named package default
  737. to the current value of *package*. Where a function takes an argument that is
  738. either a symbol or a list of symbols, an argument of nil is treated as an empty
  739. list of symbols. Any argument described as a package name may be either a
  740. string or a symbol. If a symbol is supplied, its print name will be used as the
  741. package name; if a string is supplied, the user must take care to specify the
  742. same capitalization used in the package name, normally all uppercase.
  743.  
  744. [Variable]
  745. *package*
  746.  
  747. The value of this variable must be a package; this package is said to be the
  748. current package. The initial value of *package* is the user package.
  749.  
  750. [change_begin]
  751. X3J13 voted in March 1989 (LISP-PACKAGE-NAME)   to specify that the forthcoming
  752. ANSI Common Lisp will use the package name common-lisp-user instead of user.
  753. [change_end]
  754.  
  755. The function load rebinds *package* to its current value. If some form in the
  756. file changes the value of *package* during loading, the old value will be
  757. restored when the loading is completed.
  758.  
  759. [change_begin]
  760. X3J13 voted in October 1988 (COMPILE-FILE-PACKAGE)   to require compile-file to
  761. rebind *package* to its current value.
  762. [change_end]
  763.  
  764. [Function]
  765. make-package package-name &key :nicknames :use
  766.  
  767. This creates and returns a new package with the specified package name. As
  768. described above, this argument may be either a string or a symbol. The
  769. :nicknames argument must be a list of strings to be used as alternative names
  770. for the package. Once again, the user may supply symbols in place of the
  771. strings, in which case the print names of the symbols are used. These names and
  772. nicknames must not conflict with any existing package names; if they do, a
  773. correctable error is signaled.
  774.  
  775. The :use argument is a list of packages or the names (strings or symbols) of
  776. packages whose external symbols are to be inherited by the new package. These
  777. packages must already exist. If not supplied, :use defaults to a list of one
  778. package, the lisp package.
  779.  
  780. [change_begin]
  781. X3J13 voted in March 1989 (LISP-PACKAGE-NAME)   to specify that the forthcoming
  782. ANSI Common Lisp will use the package name common-lisp instead of lisp.
  783.  
  784. X3J13 voted in January 1989 (MAKE-PACKAGE-USE-DEFAULT)   to change the
  785. specification of make-package so that the default value for the :use argument
  786. is unspecified. Portable code should specify :use '("COMMON-LISP") explicitly.
  787.  
  788. -------------------------------------------------------------------------------
  789. Rationale: Many existing implementations of Common Lisp happen to have violated
  790. the first edition specification, providing as the default not only the lisp
  791. package but also (or instead) a package containing implementation-dependent
  792. language extensions. This is for good reason: usually it is much more
  793. convenient to the user for the default :use list to be the entire,
  794. implementation-dependent, extended language rather than only the facilities
  795. specified in this book. The X3J13 vote simply legitimizes existing practice.
  796. -------------------------------------------------------------------------------
  797. [change_end]
  798.  
  799. [old_change_begin]
  800.  
  801. [Function]
  802. in-package package-name &key :nicknames :use
  803.  
  804. The in-package function is intended to be placed at the start of a file
  805. containing a subsystem that is to be loaded into some package other than user.
  806.  
  807. If there is not already a package named package-name, this function is similar
  808. to make-package, except that after the new package is created, *package* is set
  809. to it. This binding will remain in force until changed by the user (perhaps
  810. with another in-package call) or until the *package* variable reverts to its
  811. old value at the completion of a load operation.
  812.  
  813. If there is an existing package whose name is package-name, the assumption is
  814. that the user is re-loading a file after making some changes. The existing
  815. package is augmented to reflect any new nicknames or new packages in the :use
  816. list (with the usual error checking), and *package* is then set to this
  817. package.
  818. [old_change_end]
  819.  
  820. [change_begin]
  821. X3J13 voted in January 1989 (RETURN-VALUES-UNSPECIFIED)   to specify that
  822. in-package returns the new package, that is, the value of *package* after the
  823. operation has been executed.
  824.  
  825. X3J13 voted in March 1989 (LISP-PACKAGE-NAME)   to specify that the forthcoming
  826. ANSI Common Lisp will use the package name common-lisp-user instead of user.
  827.  
  828. X3J13 voted in March 1989 (IN-PACKAGE-FUNCTIONALITY)   to restrict the
  829. functionality of in-package and to make it a macro. This is an incompatible
  830. change.
  831.  
  832. Making in-package a macro rather than a function means that there is no need to
  833. require compile-file to handle it specially. Since defpackage is also defined
  834. to have side effects on the compilation environment, there is no need to
  835. require any of the package functions to be treated specially by the compiler.
  836.  
  837. [Macro]
  838. in-package name
  839.  
  840. This macro causes *package* to be set to the package named name, which must be
  841. a symbol or string. The name is not evaluated. An error is signaled if the
  842. package does not already exist. Everything this macro does is also performed at
  843. compile time if the call appears at top level.
  844. [change_end]
  845.  
  846. [Function]
  847. find-package name
  848.  
  849. The name must be a string that is the name or nickname for a package. This
  850. argument may also be a symbol, in which case the symbol's print name is used.
  851. The package with that name or nickname is returned; if no such package exists,
  852. find-package returns nil. The matching of names observes case (as in string=).
  853.  
  854. [change_begin]
  855. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to allow
  856. find-package to accept a package object, in which case the package is simply
  857. returned (see section 11.2).
  858. [change_end]
  859.  
  860. [Function]
  861. package-name package
  862.  
  863. The argument must be a package. This function returns the string that names
  864. that package.
  865.  
  866. [change_begin]
  867. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to allow
  868. package-name to accept a package name or nickname, in which case the primary
  869. name of the package so specified is returned (see section 11.2).
  870.  
  871. X3J13 voted in January 1989 (PACKAGE-DELETION)   to add a function to delete
  872. packages. One consequence of this vote is that package-name will return nil
  873. instead of a package name if applied to a deleted package object. See
  874. delete-package.
  875. [change_end]
  876.  
  877. [Function]
  878. package-nicknames package
  879.  
  880. The argument must be a package. This function returns the list of nickname
  881. strings for that package, not including the primary name.
  882.  
  883. [change_begin]
  884. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to allow
  885. package-nicknames to accept a package name or nickname, in which case the
  886. nicknames of the package so specified are returned (see section 11.2).
  887. [change_end]
  888.  
  889. [Function]
  890. rename-package package new-name &optional new-nicknames
  891.  
  892. The old name and all of the old nicknames of package are eliminated and are
  893. replaced by new-name and new-nicknames. The new-name argument is a string or
  894. symbol; the new-nicknames argument, which defaults to nil, is a list of strings
  895. or symbols.
  896.  
  897. [change_begin]
  898. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  899. the package argument may be either a package object or a package name (see
  900. section 11.2).
  901.  
  902. X3J13 voted in January 1989 (RETURN-VALUES-UNSPECIFIED)   to specify that
  903. rename-package returns package.
  904. [change_end]
  905.  
  906. [Function]
  907. package-use-list package
  908.  
  909. A list of other packages used by the argument package is returned.
  910.  
  911. [change_begin]
  912. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  913. the package argument may be either a package object or a package name (see
  914. section 11.2).
  915. [change_end]
  916.  
  917. [Function]
  918. package-used-by-list package
  919.  
  920. A list of other packages that use the argument package is returned.
  921.  
  922. [change_begin]
  923. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  924. the package argument may be either a package object or a package name (see
  925. section 11.2).
  926. [change_end]
  927.  
  928. [Function]
  929. package-shadowing-symbols package
  930.  
  931. A list is returned of symbols that have been declared as shadowing symbols in
  932. this package by shadow or shadowing-import. All symbols on this list are
  933. present in the specified package.
  934.  
  935. [change_begin]
  936. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  937. the package argument may be either a package object or a package name (see
  938. section 11.2).
  939. [change_end]
  940.  
  941. [Function]
  942. list-all-packages
  943.  
  944. This function returns a list of all packages that currently exist in the Lisp
  945. system.
  946.  
  947. [change_begin]
  948.  
  949. [Function]
  950. delete-package package
  951.  
  952. X3J13 voted in January 1989 (PACKAGE-DELETION)   to add the delete-package
  953. function, which deletes the specified package from all package system data
  954. structures. The package argument may be either a package or the name of a
  955. package.
  956.  
  957. If package is a name but there is currently no package of that name, a
  958. correctable error is signaled. Continuing from the error makes no deletion
  959. attempt but merely returns nil from the call to delete-package.
  960.  
  961. If package is a package object that has already been deleted, no error is
  962. signaled and no deletion is attempted; instead, delete-package immediately
  963. returns nil.
  964.  
  965. If the package specified for deletion is currently used by other packages, a
  966. correctable error is signaled. Continuing from this error, the effect of the
  967. function unuse-package is performed on all such other packages so as to remove
  968. their dependency on the specified package, after which delete-package proceeds
  969. to delete the specified package as if no other package had been using it.
  970.  
  971. If any symbol had the specified package as its home package before the call to
  972. delete-package, then its home package is unspecified (that is, the contents of
  973. its package cell are unspecified) after the delete-package operation has been
  974. completed. Symbols in the deleted package are not modified in any other way.
  975.  
  976. The name and nicknames of the package cease to be recognized package names. The
  977. package object is still a package, but anonymous; packagep will be true of it,
  978. but package-name applied to it will return nil.
  979.  
  980. The effect of any other package operation on a deleted package object is
  981. undefined. In particular, an attempt to locate a symbol within a deleted
  982. package (using intern or find-symbol, for example) will have unspecified
  983. results.
  984.  
  985. delete-package returns t if the deletion succeeds, and nil otherwise.
  986. [change_end]
  987.  
  988. [Function]
  989. intern string &optional package
  990.  
  991. The package, which defaults to the current package, is searched for a symbol
  992. with the name specified by the string argument. This search will include
  993. inherited symbols, as described in section 11.4. If a symbol with the specified
  994. name is found, it is returned. If no such symbol is found, one is created and
  995. is installed in the specified package as an internal symbol (as an external
  996. symbol if the package is the keyword package); the specified package becomes
  997. the home package of the created symbol.
  998.  
  999. [change_begin]
  1000. X3J13 voted in March 1989 (CHARACTER-PROPOSAL)   to specify that intern may in
  1001. effect perform the search using a copy of the argument string in which some or
  1002. all of the implementation-defined attributes have been removed from the
  1003. characters of the string. It is implementation-dependent which attributes are
  1004. removed.
  1005. [change_end]
  1006.  
  1007. Two values are returned. The first is the symbol that was found or created. The
  1008. second value is nil if no pre-existing symbol was found, and takes on one of
  1009. three values if a symbol was found:
  1010.  
  1011. :internal
  1012.      The symbol was directly present in the package as an internal symbol.
  1013.  
  1014. :external
  1015.      The symbol was directly present as an external symbol.
  1016.  
  1017. :inherited
  1018.      The symbol was inherited via use-package (which implies that the symbol is
  1019.      internal).
  1020.  
  1021. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1022. the package argument may be either a package object or a package name (see
  1023. section 11.2).
  1024.  
  1025. -------------------------------------------------------------------------------
  1026. Compatibility note: Conceptually, intern translates a string to a symbol. In
  1027. MacLisp and several other dialects of Lisp, intern can take either a string or
  1028. a symbol as its argument; in the latter case, the symbol's print name is
  1029. extracted and used as the string. However, this leads to some confusing issues
  1030. about what to do if intern finds a symbol that is not eq to the argument
  1031. symbol. To avoid such confusion, Common Lisp requires the argument to be a
  1032. string.
  1033. -------------------------------------------------------------------------------
  1034.  
  1035. [Function]
  1036. find-symbol string &optional package
  1037.  
  1038. This is identical to intern, but it never creates a new symbol. If a symbol
  1039. with the specified name is found in the specified package, directly or by
  1040. inheritance, the symbol found is returned as the first value and the second
  1041. value is as specified for intern. If the symbol is not accessible in the
  1042. specified package, both values are nil.
  1043.  
  1044. [change_begin]
  1045. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1046. the package argument may be either a package object or a package name (see
  1047. section 11.2).
  1048. [change_end]
  1049.  
  1050. [Function]
  1051. unintern symbol &optional package
  1052.  
  1053. If the specified symbol is present in the specified package, it is removed from
  1054. that package and also from the package's shadowing-symbols list if it is
  1055. present there. Moreover, if the package is the home package for the symbol, the
  1056. symbol is made to have no home package. Note that in some circumstances the
  1057. symbol may continue to be accessible in the specified package by inheritance.
  1058. unintern returns t if it actually removed a symbol, and nil otherwise.
  1059.  
  1060. unintern should be used with caution. It changes the state of the package
  1061. system in such a way that the consistency rules do not hold across the change.
  1062.  
  1063. [change_begin]
  1064. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1065. the package argument may be either a package object or a package name (see
  1066. section 11.2).
  1067. [change_end]
  1068.  
  1069. -------------------------------------------------------------------------------
  1070. Compatibility note: The equivalent of this in MacLisp is remob.
  1071. -------------------------------------------------------------------------------
  1072.  
  1073. [Function]
  1074. export symbols &optional package
  1075.  
  1076. The symbols argument should be a list of symbols, or possibly a single symbol.
  1077. These symbols become accessible as external symbols in package (see section
  1078. 11.4). export returns t.
  1079.  
  1080. By convention, a call to export listing all exported symbols is placed near the
  1081. start of a file to advertise which of the symbols mentioned in the file are
  1082. intended to be used by other programs.
  1083.  
  1084. [change_begin]
  1085. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1086. the package argument may be either a package object or a package name (see
  1087. section 11.2).
  1088. [change_end]
  1089.  
  1090. [Function]
  1091. unexport symbols &optional package
  1092.  
  1093. The argument should be a list of symbols, or possibly a single symbol. These
  1094. symbols become internal symbols in package. It is an error to unexport a symbol
  1095. from the keyword package (see section 11.4). unexport returns t.
  1096.  
  1097. [change_begin]
  1098. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1099. the package argument may be either a package object or a package name (see
  1100. section 11.2).
  1101. [change_end]
  1102.  
  1103. [Function]
  1104. import symbols &optional package
  1105.  
  1106. The argument should be a list of symbols, or possibly a single symbol. These
  1107. symbols become internal symbols in package and can therefore be referred to
  1108. without having to use qualified-name (colon) syntax. import signals a
  1109. correctable error if any of the imported symbols has the same name as some
  1110. distinct symbol already accessible in the package (see section 11.4). import
  1111. returns t.
  1112.  
  1113. [change_begin]
  1114. X3J13 voted in June 1987 (IMPORT-SETF-SYMBOL-PACKAGE)   to clarify that if any
  1115. symbol to be imported has no home package then import sets the home package of
  1116. the symbol to the package to which the symbol is being imported.
  1117.  
  1118. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1119. the package argument may be either a package object or a package name (see
  1120. section 11.2).
  1121. [change_end]
  1122.  
  1123. [Function]
  1124. shadowing-import symbols &optional package
  1125.  
  1126. This is like import, but it does not signal an error even if the importation of
  1127. a symbol would shadow some symbol already accessible in the package. In
  1128. addition to being imported, the symbol is placed on the shadowing-symbols list
  1129. of package (see section 11.5). shadowing-import returns t.
  1130.  
  1131. shadowing-import should be used with caution. It changes the state of the
  1132. package system in such a way that the consistency rules do not hold across the
  1133. change.
  1134.  
  1135. [change_begin]
  1136. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1137. the package argument may be either a package object or a package name (see
  1138. section 11.2).
  1139. [change_end]
  1140.  
  1141. [Function]
  1142. shadow symbols &optional package
  1143.  
  1144. The argument should be a list of symbols, or possibly a single symbol. The
  1145. print name of each symbol is extracted, and the specified package is searched
  1146. for a symbol of that name. If such a symbol is present in this package
  1147. (directly, not by inheritance), then nothing is done. Otherwise, a new symbol
  1148. is created with this print name, and it is inserted in the package as an
  1149. internal symbol. The symbol is also placed on the shadowing-symbols list of the
  1150. package (see section 11.5). shadow returns t.
  1151.  
  1152. [change_begin]
  1153. X3J13 voted in March 1988 (SHADOW-ALREADY-PRESENT)   to change shadow to accept
  1154. strings as well as well as symbols (a string in the symbols list being treated
  1155. as a print name), and to clarify that if a symbol of specified name is already
  1156. in the package but is not yet on the shadowing-symbols list for that package,
  1157. then shadow does add it to the shadowing-symbols list rather than simply doing
  1158. nothing.
  1159. [change_end]
  1160.  
  1161. shadow should be used with caution. It changes the state of the package system
  1162. in such a way that the consistency rules do not hold across the change.
  1163.  
  1164. [change_begin]
  1165. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1166. the package argument may be either a package object or a package name (see
  1167. section 11.2).
  1168. [change_end]
  1169.  
  1170. [Function]
  1171. use-package packages-to-use &optional package
  1172.  
  1173. The packages-to-use argument should be a list of packages or package names, or
  1174. possibly a single package or package name. These packages are added to the
  1175. use-list of package if they are not there already. All external symbols in the
  1176. packages to use become accessible in package as internal symbols (see section
  1177. 11.4). It is an error to try to use the keyword package. use-package returns t.
  1178.  
  1179. [change_begin]
  1180. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1181. the package argument may be either a package object or a package name (see
  1182. section 11.2).
  1183. [change_end]
  1184.  
  1185. [Function]
  1186. unuse-package packages-to-unuse &optional package
  1187.  
  1188. The packages-to-unuse argument should be a list of packages or package names,
  1189. or possibly a single package or package name. These packages are removed from
  1190. the use-list of package. unuse-package returns t.
  1191.  
  1192. [change_begin]
  1193. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1194. the package argument may be either a package object or a package name (see
  1195. section 11.2).
  1196.  
  1197. X3J13 voted in January 1989 (DEFPACKAGE)   to add a macro defpackage to the
  1198. language to make it easier to create new packages, alleviating the burden on
  1199. the programmer to perform the various setup operations in exactly the correct
  1200. sequence.
  1201.  
  1202. [Macro]
  1203. defpackage defined-package-name {option}*
  1204.  
  1205. This creates a new package, or modifies an existing one, whose name is
  1206. defined-package-name. The defined-package-name may be a string or a symbol; if
  1207. it is a symbol, only its print name matters, and not what package, if any, the
  1208. symbol happens to be in. The newly created or modified package is returned as
  1209. the value of the defpackage form.
  1210.  
  1211. Each standard option is a list of a keyword (the name of the option) and
  1212. associated arguments. No part of a defpackage form is evaluated. Except for the
  1213. :size option, more than one option of the same kind may occur within the same
  1214. defpackage form.
  1215.  
  1216. The standard options for defpackage are as follows. In every case, any option
  1217. argument called package-name or symbol-name may be a string or a symbol; if it
  1218. is a symbol, only its print name matters, and not what package, if any, the
  1219. symbol happens to be in.
  1220.  
  1221. (:size integer)
  1222.      This specifies approximately the number of symbols expected to be in the
  1223.      package. This is purely an efficiency hint to the storage allocator, so
  1224.      that implementations using hash tables as part of the package data
  1225.      structure (the usual technique) will not have to incrementally expand the
  1226.      package as new symbols are added to it (for example, as a large file is
  1227.      read while ``in'' that package).
  1228.  
  1229. (:nicknames {package-name}*)
  1230.      The specified names become nicknames of the package being defined. If any
  1231.      of the specified nicknames already refers to an existing package, a
  1232.      continuable error is signaled exactly as for the function make-package.
  1233.  
  1234. (:shadow {symbol-name}*)
  1235.      Symbols with the specified names are created as shadows in the package
  1236.      being defined, just as with the function shadow.
  1237.  
  1238. (:shadowing-import-from package-name {symbol-name}*)
  1239.      Symbols with the specified names are located in the specified package.
  1240.      These symbols are imported into the package being defined, shadowing other
  1241.      symbols if necessary, just as with the function shadowing-import. In no
  1242.      case will symbols be created in a package other than the one being
  1243.      defined; a continuable error is signaled if for any symbol-name there is
  1244.      no symbol of that name accessible in the package named package-name.
  1245.  
  1246. (:use {package-name}*)
  1247.      The package being defined is made to ``use'' (inherit from) the packages
  1248.      specified by this option, just as with the function use-package. If no
  1249.      :use option is supplied, then a default list is assumed as for
  1250.      make-package.
  1251.  
  1252.      X3J13 voted in January 1989 (MAKE-PACKAGE-USE-DEFAULT)   to change the
  1253.      specification of make-package so that the default value for the :use
  1254.      argument is unspecified. This change affects defpackage as well. Portable
  1255.      code should specify (:use '("COMMON-LISP")) explicitly.
  1256.  
  1257. (:import-from package-name {symbol-name}*)
  1258.      Symbols with the specified names are located in the specified package.
  1259.      These symbols are imported into the package being defined, just as with
  1260.      the function import. In no case will symbols be created in a package other
  1261.      than the one being defined; a continuable error is signaled if for any
  1262.      symbol-name there is no symbol of that name accessible in the package
  1263.      named package-name.
  1264.  
  1265. (:intern {symbol-name}*)
  1266.      Symbols with the specified names are located or created in the package
  1267.      being defined, just as with the function intern. Note that the action of
  1268.      this option may be affected by a :use option, because an inherited symbol
  1269.      will be used in preference to creating a new one.
  1270.  
  1271. (:export {symbol-name}*)
  1272.      Symbols with the specified names are located or created in the package
  1273.      being defined and then exported, just as with the function export. Note
  1274.      that the action of this option may be affected by a :use, :import-from, or
  1275.      :shadowing-import-from option, because an inherited or imported symbol
  1276.      will be used in preference to creating a new one.
  1277.  
  1278. The order in which options appear in a defpackage form does not matter; part of
  1279. the convenience of defpackage is that it sorts out the options into the correct
  1280. order for processing. Options are processed in the following order:
  1281.  
  1282.   1.  :shadow and :shadowing-import-from
  1283.   2.  :use
  1284.   3.  :import-from and :intern
  1285.   4.  :export
  1286.  
  1287. Shadows are established first in order to avoid spurious name conflicts when
  1288. use links are established. Use links must occur before importing and interning
  1289. so that those operations may refer to normally inherited symbols rather than
  1290. creating new ones. Exports are performed last so that symbols created by any of
  1291. the other options, in particular, shadows and imported symbols, may be
  1292. exported. Note that exporting an inherited symbol implicitly imports it first
  1293. (see section 11.4).
  1294.  
  1295. If no package named defined-package-name already exists, defpackage creates it.
  1296. If such a package does already exist, then no new package is created. The
  1297. existing package is modified, if possible, to reflect the new definition. The
  1298. results are undefined if the new definition is not consistent with the current
  1299. state of the package.
  1300.  
  1301. An error is signaled if more than one :size option appears.
  1302.  
  1303. An error is signaled if the same symbol-name argument (in the sense of
  1304. comparing names with string=) appears more than once among the arguments to all
  1305. the :shadow, :shadowing-import-from, :import-from, and :intern options.
  1306.  
  1307. An error is signaled if the same symbol-name argument (in the sense of
  1308. comparing names with string=) appears more than once among the arguments to all
  1309. the :intern and :export options.
  1310.  
  1311. Other kinds of name conflicts are handled in the same manner that the
  1312. underlying operations use-package, import, and export would handle them.
  1313.  
  1314. Implementations may support other defpackage options. Every implementation
  1315. should signal an error on encountering a defpackage option it does not support.
  1316.  
  1317. The function compile-file should treat top-level defpackage forms in the same
  1318. way it would treat top-level calls to package-affecting functions (as described
  1319. at the beginning of section 11.7).
  1320.  
  1321. Here is an example of a call to defpackage that ``plays it safe'' by using only
  1322. strings as names.
  1323.  
  1324. (cl:defpackage "MY-VERY-OWN-PACKAGE"
  1325.   (:size 496)
  1326.   (:nicknames "MY-PKG" "MYPKG" "MVOP")
  1327.   (:use "COMMON-LISP")
  1328.   (:shadow "CAR" "CDR")
  1329.   (:shadowing-import-from "BRAND-X-LISP" "CONS")
  1330.   (:import-from "BRAND-X-LISP" "GC" "BLINK-FRONT-PANEL-LIGHTS")
  1331.   (:export "EQ" "CONS" "MY-VERY-OWN-FUNCTION"))
  1332.  
  1333. The preceding defpackage example is designed to operate correctly even if the
  1334. package current when the form is read happens not to ``use'' the common-lisp
  1335. package. (Note the use in this example of the nickname cl for the common-lisp
  1336. package.) Moreover, neither reading in nor evaluating this defpackage form will
  1337. ever create any symbols in the current package. Note too the use of uppercase
  1338. letters in the strings.
  1339.  
  1340. Here, for the sake of contrast, is a rather similar use of defpackage that
  1341. ``plays the whale'' by using all sorts of permissible syntax.
  1342.  
  1343. (defpackage my-very-own-package
  1344.   (:export :EQ common-lisp:cons my-very-own-function)
  1345.   (:nicknames "MY-PKG" #:MyPkg)
  1346.   (:use "COMMON-LISP")
  1347.   (:shadow "CAR")
  1348.   (:size 496)
  1349.   (:nicknames mvop)
  1350.   (:import-from "BRAND-X-LISP" "GC" Blink-Front-Panel-Lights)
  1351.   (:shadow common-lisp::cdr)
  1352.   (:shadowing-import-from "BRAND-X-LISP" CONS))
  1353.  
  1354. This example has exactly the same effect on the newly created package but may
  1355. create useless symbols in other packages. The use of explicit package tags is
  1356. particularly confusing; for example, this defpackage form will cause the symbol
  1357. cdr to be shadowed in the new package; it will not be shadowed in the package
  1358. common-lisp. The fact that the name ``CDR'' was specified by a
  1359. package-qualified reference to a symbol in the common-lisp package is a red
  1360. herring. The moral is that the syntactic flexibility of defpackage, as in other
  1361. parts of Common Lisp, yields considerable convenience when used with
  1362. commonsense competence, but unutterable confusion when used with Malthusian
  1363. profusion.
  1364.  
  1365. -------------------------------------------------------------------------------
  1366. Implementation note: An implementation of defpackage might choose to transform
  1367. all the package-name and symbol-name arguments into strings at macro expansion
  1368. time, rather than at the time the resulting expansion is executed, so that even
  1369. if source code is expressed in terms of strange symbols in the defpackage form,
  1370. the binary file resulting from compiling the source code would contain only
  1371. strings. The purpose of this is simply to minimize the creation of useless
  1372. symbols in production code. This technique is permitted as an implementation
  1373. strategy but is not a behavior required by the specification of defpackage.
  1374. -------------------------------------------------------------------------------
  1375.  
  1376. Note that defpackage is not capable by itself of defining mutually recursive
  1377. packages, for example two packages each of which uses the other. However,
  1378. nothing prevents one from using defpackage to perform much of the initial setup
  1379. and then using functions such as use-package, import, and export to complete
  1380. the links.
  1381.  
  1382. The purpose of defpackage is to encourage the user to put the entire definition
  1383. of a package and its relationships to other packages in a single place. It may
  1384. also encourage the designer of a large system to place the definitions of all
  1385. relevant packages into a single file (say) that can be loaded before loading or
  1386. compiling any code that depends on those packages. Such a file, if carefully
  1387. constructed, can simply be loaded into the common-lisp-user package.
  1388.  
  1389. Implementations and programming environments may also be better able to support
  1390. the programming process (if only by providing better error checking) through
  1391. global knowledge of the intended package setup.
  1392. [change_end]
  1393.  
  1394. [Function]
  1395. find-all-symbols string-or-symbol
  1396.  
  1397. find-all-symbols searches every package in the Lisp system to find every symbol
  1398. whose print name is the specified string. A list of all such symbols found is
  1399. returned. This search is case-sensitive. If the argument is a symbol, its print
  1400. name supplies the string to be searched for.
  1401.  
  1402. [Macro]
  1403.  
  1404. do-symbols (var [package [result-form]])
  1405.             {declaration}* {tag | statement}*
  1406.  
  1407. do-symbols provides straightforward iteration over the symbols of a package.
  1408. The body is performed once for each symbol accessible in the package, in no
  1409. particular order, with the variable var bound to the symbol. Then result-form
  1410. (a single form, not an implicit progn) is evaluated, and the result is the
  1411. value of the do-symbols form. (When the result-form is evaluated, the control
  1412. variable var is still bound and has the value nil.) If the result-form is
  1413. omitted, the result is nil. return may be used to terminate the iteration
  1414. prematurely. If execution of the body affects which symbols are contained in
  1415. the package, other than possibly to remove the symbol currently the value of
  1416. var by using unintern, the effects are unpredictable.
  1417.  
  1418. [change_begin]
  1419. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1420. the package argument may be either a package object or a package name (see
  1421. section 11.2).
  1422.  
  1423. X3J13 voted in March 1988 (DO-SYMBOLS-DUPLICATES)   to specify that the body of
  1424. a do-symbols form may be executed more than once for the same accessible
  1425. symbol, and users should take care to allow for this possibility.
  1426.  
  1427. The point is that the same symbol might be accessible via more than one chain
  1428. of inheritance, and it is implementationally costly to eliminate such
  1429. duplicates. Here is an example:
  1430.  
  1431. (setq *a* (make-package 'a))      ;Implicitly uses package common-lisp
  1432. (setq *b* (make-package 'b))      ;Implicitly uses package common-lisp
  1433. (setq *c* (make-package 'c :use '(a b)))
  1434.  
  1435. (do-symbols (x *c*) (print x))    ;Symbols in package common-lisp
  1436.                                   ; might be printed once or twice here
  1437.  
  1438. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1439. user side effects; see section 7.9.
  1440.  
  1441. Note that the loop construct provides a kind of for clause that can iterate
  1442. over the symbols of a package (see chapter 26).
  1443. [change_end]
  1444.  
  1445. [Macro]
  1446.  
  1447. do-external-symbols (var [package [result]])
  1448.                       {declaration}* {tag | statement}*
  1449.  
  1450. do-external-symbols is just like do-symbols, except that only the external
  1451. symbols of the specified package are scanned. The clarification voted by X3J13
  1452. in March 1988 for do-symbols (DO-SYMBOLS-DUPLICATES)   , regarding redundant
  1453. executions of the body for the same symbol, applies also to
  1454. do-external-symbols.
  1455.  
  1456. [change_begin]
  1457. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1458. the package argument may be either a package object or a package name (see
  1459. section 11.2).
  1460.  
  1461. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1462. user side effects; see section 7.9.
  1463. [change_end]
  1464.  
  1465. [Macro]
  1466.  
  1467. do-all-symbols (var [result-form])
  1468.                  {declaration}* {tag | statement}*
  1469.  
  1470. This is similar to do-symbols but executes the body once for every symbol
  1471. contained in every package. (This will not process every symbol whatsoever,
  1472. because a symbol not accessible in any package will not be processed. Normally,
  1473. uninterned symbols are not accessible in any package.) It is not in general the
  1474. case that each symbol is processed only once, because a symbol may appear in
  1475. many packages.
  1476.  
  1477. [change_begin]
  1478. The clarification voted by X3J13 in March 1988 for do-symbols
  1479. (DO-SYMBOLS-DUPLICATES)   , regarding redundant executions of the body for the
  1480. same symbol, applies also to do-all-symbols.
  1481.  
  1482. X3J13 voted in January 1989 (PACKAGE-FUNCTION-CONSISTENCY)   to clarify that
  1483. the package argument may be either a package object or a package name (see
  1484. section 11.2).
  1485.  
  1486. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1487. user side effects; see section 7.9.
  1488.  
  1489. X3J13 voted in January 1989 (HASH-TABLE-PACKAGE-GENERATORS)   to add a new
  1490. macro with-package-iterator to the language.
  1491.  
  1492. [Macro]
  1493.  
  1494. with-package-iterator (mname package-list {symbol-type}+)
  1495.                         {form}*
  1496.  
  1497. The name mname is bound and defined as if by macrolet, with the body forms as
  1498. its lexical scope, to be a ``generator macro'' such that each invocation of
  1499. (mname) will return a symbol and that successive invocations will eventually
  1500. deliver, one by one, all the symbols from the packages that are elements of the
  1501. list that is the value of the expression package-list (which is evaluated
  1502. exactly once).
  1503.  
  1504. Each element of the package-list value may be either a package or the name of a
  1505. package. As a further convenience, if the package-list value is itself a
  1506. package or the name of a package, it is treated as if a singleton list
  1507. containing that value had been provided. If the package-list value is nil, it
  1508. is considered to be an empty list of packages.
  1509.  
  1510. At each invocation of the generator macro, there are two possibilities. If
  1511. there is yet another unprocessed symbol, then four values are returned: t, the
  1512. symbol, a keyword indicating the accessibility of the symbol within the package
  1513. (see below), and the package from which the symbol was accessed. If there are
  1514. no more unprocessed symbols in the list of packages, then one value is
  1515. returned: nil.
  1516.  
  1517. When the generator macro returns a symbol as its second value, the fourth value
  1518. is always one of the packages present or named in the package-list value, and
  1519. the third value is a keyword indicating accessibility: :internal means present
  1520. in the package and not exported; :external means present and exported; and
  1521. :inherited means not present (thus not shadowed) but inherited from some
  1522. package used by the package that is the fourth value.
  1523.  
  1524. Each symbol-type in an invocation of with-package-iterator is not evaluated.
  1525. More than one may be present; their order does not matter. They indicate the
  1526. accessibility types of interest. A symbol is not returned by the generator
  1527. macro unless its actual accessibility matches one of the symbol-type
  1528. indicators. The standard symbol-type indicators are :internal, :external, and
  1529. :inherited, but implementations are permitted to extend the syntax of
  1530. with-package-iterator by recognizing additional symbol accessibility types. An
  1531. error is signaled if no symbol-type is supplied, or if any supplied symbol-type
  1532. is not recognized by the implementation.
  1533.  
  1534. The order in which symbols are produced by successive invocations of the
  1535. generator macro is not necessarily correlated in any way with the order of the
  1536. packages in the package-list. When more than one package is in the
  1537. package-list, symbols accessible from more than one package may be produced
  1538. once or more than once. Even when only one package is specified, symbols
  1539. inherited in multiple ways via used packages may be produced once or more than
  1540. once.
  1541.  
  1542. The implicit interior state of the iteration over the list of packages and the
  1543. symbols within them has dynamic extent. It is an error to invoke the generator
  1544. macro once the with-package-iterator form has been exited.
  1545.  
  1546. Any number of invocations of with-package-iterator and related macros may be
  1547. nested, and the generator macro of an outer invocation may be called from
  1548. within an inner invocation (provided, of course, that its name is visible or
  1549. otherwise made available).
  1550.  
  1551. X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict
  1552. user side effects; see section 7.9.
  1553.  
  1554. -------------------------------------------------------------------------------
  1555. Rationale: This facility is a bit more flexible in some ways than do-symbols
  1556. and friends. In particular, it makes it possible to implement loop clauses for
  1557. iterating over packages in a way that is both portable and efficient (see
  1558. chapter 26).
  1559. -------------------------------------------------------------------------------
  1560.  
  1561. [change_end]
  1562.  
  1563. -------------------------------------------------------------------------------
  1564.  
  1565. 11.8. Modules
  1566.  
  1567. A module is a Common Lisp subsystem that is loaded from one or more files. A
  1568. module is normally loaded as a single unit, regardless of how many files are
  1569. involved. A module may consist of one package or several packages. The
  1570. file-loading process is necessarily implementation-dependent, but Common Lisp
  1571. provides some very simple portable machinery for naming modules, for keeping
  1572. track of which modules have been loaded, and for loading modules as a unit.
  1573.  
  1574. [change_begin]
  1575. X3J13 voted in January 1989 (REQUIRE-PATHNAME-DEFAULTS)   to eliminate the
  1576. entire module facility from the language; that is, the variable *modules* and
  1577. the functions provide and require are deleted. X3J13 commented that the
  1578. file-loading feature of require is not portable, and that the remaining
  1579. functionality is easily implemented by user code. (I will add that in any case
  1580. the specification of require is so vague that different implementations are
  1581. likely to have differing behavior.)
  1582. [change_end]
  1583.  
  1584. [Variable]
  1585. *modules*
  1586.  
  1587. The variable *modules* is a list of names of the modules that have been loaded
  1588. into the Lisp system so far. This list is used by the functions provide and
  1589. require.
  1590.  
  1591. [Function]
  1592. provide module-name
  1593. require module-name &optional pathname
  1594.  
  1595. Each module has a unique name (a string). The provide and require functions
  1596. accept either a string or a symbol as the module-name argument. If a symbol is
  1597. provided, its print name is used as the module name. If the module consists of
  1598. a single package, it is customary for the package and module names to be the
  1599. same.
  1600.  
  1601. The provide function adds a new module name to the list of modules maintained
  1602. in the variable *modules*, thereby indicating that the module in question has
  1603. been loaded.
  1604.  
  1605. The require function tests whether a module is already present (using a
  1606. case-sensitive comparison); if the module is not present, require proceeds to
  1607. load the appropriate file or set of files. The pathname argument, if present,
  1608. is a single pathname or a list of pathnames whose files are to be loaded in
  1609. order, left to right. If the pathname argument is nil or is not provided, the
  1610. system will attempt to determine, in some system-dependent manner, which files
  1611. to load. This will typically involve some central registry of module names and
  1612. the associated file lists.
  1613.  
  1614. [change_begin]
  1615. X3J13 voted in March 1988 not to permit symbols as pathnames (PATHNAME-SYMBOL)
  1616.   and to specify exactly which streams may be used as pathnames
  1617. (PATHNAME-STREAM)   (see section 23.1.6). Of course, this is moot if require is
  1618. not in the language.
  1619.  
  1620. X3J13 voted in January 1989 (RETURN-VALUES-UNSPECIFIED)   to specify that the
  1621. values returned by provide and require are implementation-dependent. Of course,
  1622. this is moot if provide and require are not in the language.
  1623. [change_end]
  1624.  
  1625. -------------------------------------------------------------------------------
  1626. Implementation note: One way to implement such a registry on many operating
  1627. systems is simply to use a distinguished ``library'' directory within the file
  1628. system, where the name of each file is the same as the module it contains.
  1629. -------------------------------------------------------------------------------
  1630.  
  1631. 11.9. An Example
  1632.  
  1633. [old_change_begin]
  1634. Most users will want to load and use packages but will never need to build one.
  1635. Often a user will load a number of packages into the user package whenever
  1636. using Common Lisp. Typically an implementation might provide some sort of
  1637. initialization file mechanism to make such setup automatic when the Lisp starts
  1638. up. Table 11-1 shows such an initialization file, one that simply causes other
  1639. facilities to be loaded.
  1640. [old_change_end]
  1641.  
  1642. [change_begin]
  1643. X3J13 voted in March 1989 (LISP-PACKAGE-NAME)   to specify that the forthcoming
  1644. ANSI Common Lisp will use the package name common-lisp-user instead of user.
  1645. [change_end]
  1646.  
  1647.  
  1648.  
  1649. ----------------------------------------------------------------
  1650. Table 11-1: An Initialization File
  1651.  
  1652. ;;;; Lisp init file for I. Newton.
  1653.  
  1654. ;;; Set up the USER package the way I like it.
  1655.  
  1656. (require 'calculus)             ;I use CALCULUS a lot; load it.
  1657. (use-package 'calculus)         ;Get easy access to its
  1658.                                 ; exported symbols.
  1659.  
  1660. (require 'newtonian-mechanics)  ;Same thing for NEWTONIAN-MECHANICS
  1661. (use-package 'newtonian-mechanics)
  1662.  
  1663. ;;; I just want a few things from RELATIVITY,
  1664. ;;; and other things conflict.
  1665. ;;; Import only what I need into the USER package.
  1666.  
  1667. (require 'relativity)
  1668. (import '(relativity:speed-of-light
  1669.           relativity:ignore-small-errors))
  1670.  
  1671. ;;; These are worth loading, but I will use qualified names,
  1672. ;;; such as PHLOGISTON:MAKE-FIRE-BOTTLE, to get at any symbols
  1673. ;;; I might need from these packages.
  1674.  
  1675. (require 'phlogiston)
  1676. (require 'alchemy)
  1677.  
  1678. ;;; End of Lisp init file for I. Newton.
  1679.  
  1680. ----------------------------------------------------------------
  1681.  
  1682. When each of two files uses some symbols from the other, the author of those
  1683. files must be careful to arrange the contents of the file in the proper order.
  1684. Typically each file contains a single package that is a complete module. The
  1685. contents of such a file should include the following items, in order:
  1686.  
  1687.   1.  A call to provide that announces the module name.
  1688.  
  1689.   2.  A call to in-package that establishes the package.
  1690.  
  1691.   3.  A call to shadow that establishes any local symbols that will shadow
  1692.      symbols that would otherwise be inherited from packages that this package
  1693.      will use.
  1694.  
  1695.   4.  A call to export that establishes all of this package's external symbols.
  1696.  
  1697.   5.  Any number of calls to require to load other modules that the contents of
  1698.      this file might want to use or refer to. (Because the calls to require
  1699.      follow the calls to in-package, shadow, and export, it is possible for the
  1700.      packages that may be loaded to refer to external symbols in this package.)
  1701.  
  1702.   6.  Any number of calls to use-package, to make external symbols from other
  1703.      packages accessible in this package.
  1704.  
  1705.   7.  Any number of calls to import, to make symbols from other packages
  1706.      present in this package.
  1707.  
  1708.   8.  Finally, the definitions making up the contents of this package/module.
  1709.  
  1710. The following mnemonic sentence may be helpful in remembering the proper order
  1711. of these calls:
  1712.  
  1713.      Put in seven extremely random user interface commands.
  1714.  
  1715. Each word of the sentence corresponds to one item in the above ordering:
  1716.  
  1717.            Put             Provide
  1718.            IN              IN-package
  1719.            Seven           Shadow
  1720.            EXtremely       EXport
  1721.            Random          Require
  1722.            USEr            USE-package
  1723.            Interface       Import
  1724.            COmmands        COntents of package/module
  1725.  
  1726. The sentence says what it helps you to do.
  1727.  
  1728. [change_begin]
  1729. The most distressing aspect of the X3J13 vote to eliminate provide and require
  1730. (REQUIRE-PATHNAME-DEFAULTS)   is of course that it completely ruins the
  1731. mnemonic sentence.
  1732. [change_end]
  1733.  
  1734. Now, suppose for the sake of example that the phlogiston and alchemy packages
  1735. are single-file, single-package modules as described above. The phlogiston
  1736. package needs to use the alchemy package, and the alchemy package needs to use
  1737. several external symbols from the phlogiston package. The definitions in the
  1738. alchemy and phlogiston files (see tables 11-2 and 11-3) allow a user to specify
  1739. require statements for either of these modules, or for both of them in either
  1740. order, and all relevant information will be loaded automatically and in the
  1741. correct order.
  1742.  
  1743.  
  1744.  
  1745. ----------------------------------------------------------------
  1746. Table 11-2: File alchemy
  1747.  
  1748. ;;;; Alchemy functions, written and maintained by Merlin, Inc.
  1749.  
  1750. (provide 'alchemy)                  ;The module is named ALCHEMY.
  1751. (in-package 'alchemy)               ;So is the package.
  1752.  
  1753. ;;; There is nothing to shadow.
  1754.  
  1755. ;;; Here is the external interface.
  1756.  
  1757. (export '(lead-to-gold gold-to-lead
  1758.           antimony-to-zinc elixir-of-life))
  1759.  
  1760. ;;; This package/module needs a function from
  1761. ;;; the PHLOGISTON package/module.
  1762.  
  1763. (require 'phlogiston)
  1764.  
  1765. ;;; We don't frequently need most of the external symbols from
  1766. ;;; PHLOGISTON, so it's not worth doing a USE-PACKAGE on it.
  1767. ;;; We'll just use qualified names as needed.  But we use
  1768. ;;; one function, MAKE-FIRE-BOTTLE, a lot, so import it.
  1769. ;;; It's external in PHLOGISTON and so can be referred to
  1770. ;;; here using ":" qualified-name syntax.
  1771.  
  1772. (import '(phlogiston:make-fire-bottle))
  1773.  
  1774. ;;; Now for the real contents of this file.
  1775.  
  1776. (defun lead-to-gold (x)
  1777.   "Takes a quantity of lead and returns gold."
  1778.   (when (> (phlogiston:heat-flow 5 x x)  ;Using a qualified symbol
  1779.            3)
  1780.     (make-fire-bottle x))                ;Using an imported symbol
  1781.   (gild x))
  1782.  
  1783. ;;; And so on ...
  1784.  
  1785. ----------------------------------------------------------------
  1786.  
  1787.  
  1788.  
  1789. ----------------------------------------------------------------
  1790. Table 11-3: File phlogiston
  1791.  
  1792. ;;;; Phlogiston functions, by Thermofluidics, Ltd.
  1793.  
  1794. (provide 'phlogiston)             ;The module is named PHLOGISTON.
  1795. (in-package 'phlogiston)          ;So is the package.
  1796.  
  1797. ;;; There is nothing to shadow.
  1798.  
  1799. ;;; Here is the external interface.
  1800.  
  1801. (export '(heat-flow cold-flow mix-fluids separate-fluids
  1802.           burn make-fire-bottle))
  1803.  
  1804. ;;; This file uses functions from the ALCHEMY package/module.
  1805.  
  1806. (require 'alchemy)
  1807.  
  1808. ;;; We use alchemy functions a lot, so use the package.
  1809. ;;; This will allow symbols exported from the ALCHEMY package
  1810. ;;; to be referred to here without the need for qualified names.
  1811.  
  1812. (use-package 'alchemy)
  1813.  
  1814. ;;; No calls to IMPORT are needed here.
  1815.  
  1816. ;;; The real contents of this package/module.
  1817.  
  1818. (defvar *feeling-weak* nil)
  1819.  
  1820. (defun heat-flow (amount x y)
  1821.   "Make some amount of heat flow from x to y."
  1822.   (when *feeling-weak*
  1823.     (quaff (elixir-of-life)))     ;No qualifier is needed.
  1824.   (push-heat amount x y))
  1825.  
  1826. ;;; And so on ...
  1827.  
  1828. ----------------------------------------------------------------
  1829.  
  1830. [old_change_begin]
  1831. For very large modules whose contents are spread over several files (the lisp
  1832. package is an example), it is recommended that the user create the package and
  1833. declare all of the shadows and external symbols in a separate file, so that
  1834. this can be loaded before anything that might use symbols from this package.
  1835. [old_change_end]
  1836.  
  1837. [change_begin]
  1838. Indeed, the defpackage macro approved by X3J13 in January 1989 (DEFPACKAGE)
  1839. encourages the use of such a separate file. (By the way, X3J13 voted in March
  1840. 1989 (LISP-PACKAGE-NAME)   to specify that the forthcoming ANSI Common Lisp
  1841. will use the package name common-lisp instead of lisp.) Let's take a look at a
  1842. revision of I. Newton's files using defpackage.
  1843.  
  1844. The new version of the initialization file avoids using require; instead, we
  1845. assume that load will do the job (see table 11-4).
  1846.  
  1847.  
  1848.  
  1849. ----------------------------------------------------------------
  1850. Table 11-4: An Initialization File When defpackage Is Used
  1851.  
  1852. ;;;; Lisp init file for I. Newton.
  1853.  
  1854. ;;; Set up the USER package the way I like it.
  1855.  
  1856. (load "calculus")               ;I use CALCULUS a lot; load it.
  1857. (use-package 'calculus)         ;Get easy access to its
  1858.                                 ; exported symbols.
  1859.  
  1860. (load "newtonian-mechanics")    ;Ditto for NEWTONIAN-MECHANICS
  1861. (use-package 'newtonian-mechanics)
  1862.  
  1863. ;;; I just want a few things from RELATIVITY,
  1864. ;;; and other things conflict.
  1865. ;;; Import only what I need into the USER package.
  1866.  
  1867. (load "relativity")
  1868. (import '(relativity:speed-of-light
  1869.           relativity:ignore-small-errors))
  1870.  
  1871. ;;; These are worth loading, but I will use qualified names,
  1872. ;;; such as PHLOGISTON:MAKE-FIRE-BOTTLE, to get at any symbols
  1873. ;;; I might need from these packages.
  1874.  
  1875. (load "phlogiston")
  1876. (load "alchemy")
  1877.  
  1878. ;;; End of Lisp init file for I. Newton.
  1879.  
  1880. ----------------------------------------------------------------
  1881.  
  1882. The other files have each been split into two parts, one that establishes the
  1883. package and one that defines the contents. This example uses a simple
  1884. convention that for any file named, say, ``foo'' the file named ``foo-package''
  1885. contains the necessary defpackage and/or other package-establishing code. The
  1886. idiom
  1887.  
  1888. (unless (find-package "FOO")
  1889.   (load "foo-package"))
  1890.  
  1891. is conventionally used to load a package definition but only if the package has
  1892. not already been defined. (This is a bit clumsy, and there are other ways to
  1893. arrange things so that a package is defined no more than once.)
  1894.  
  1895. The file alchemy-package is shown in table 11-5. The tricky point here is that
  1896. the alchemy and phlogiston packages contain mutual references (each imports
  1897. from the other), and so defpackage alone cannot do the job. Therefore the
  1898. phlogiston package is not mentioned in a :use option in the defpackage for the
  1899. alchemy package. Instead, the function use-package is called explicitly, after
  1900. the package definition for phlogiston has been loaded. Note that this file has
  1901. been coded with excruciating care so as to operate correctly even if the
  1902. package current when the file is loaded does not inherit from the common-lisp
  1903. package. In particular, the standard load-package-definition idiom has been
  1904. peppered with package qualifiers:
  1905.  
  1906. (cl:unless (cl:find-package "PHLOGISTON")
  1907.   (cl:load "phlogiston-package"))
  1908.  
  1909. Note the use of the nickname cl for the common-lisp package.
  1910.  
  1911. The alchemy file, shown in table 11-6, simply loads the alchemy package
  1912. definition, makes that package current, and then defines the ``real contents''
  1913. of the package.
  1914.  
  1915.  
  1916.  
  1917. ----------------------------------------------------------------
  1918. Table 11-5: File alchemy-package Using defpackage
  1919.  
  1920. ;;;; Alchemy package, written and maintained by Merlin, Inc.
  1921.  
  1922. (cl:defpackage "ALCHEMY"
  1923.   (:export "LEAD-TO-GOLD" "GOLD-TO-LEAD"
  1924.            "ANTIMONY-TO-ZINC" "ELIXIR-OF-LIFE")
  1925.   )
  1926.  
  1927. ;;; This package needs a function from the PHLOGISTON package.
  1928. ;;; Load the definition of the PHLOGISTON package if necessary.
  1929.  
  1930. (cl:unless (cl:find-package "PHLOGISTON")
  1931.   (cl:load "phlogiston-package"))
  1932.  
  1933. ;;; We don't frequently need most of the external symbols from
  1934. ;;; PHLOGISTON, so it's not worth doing a USE-PACKAGE on it.
  1935. ;;; We'll just use qualified names as needed.  But we use
  1936. ;;; one function, MAKE-FIRE-BOTTLE, a lot, so import it.
  1937. ;;; It's external in PHLOGISTON and so can be referred to
  1938. ;;; here using ":" qualified-name syntax.
  1939.  
  1940. (cl:import '(phlogiston:make-fire-bottle))
  1941.  
  1942. ----------------------------------------------------------------
  1943.  
  1944.  
  1945.  
  1946. ----------------------------------------------------------------
  1947. Table 11-6: File alchemy Using defpackage
  1948.  
  1949. ;;;; Alchemy functions, written and maintained by Merlin, Inc.
  1950.  
  1951. (unless (find-package "ALCHEMY")
  1952.   (load "alchemy-package"))
  1953.  
  1954. (in-package 'alchemy)
  1955.  
  1956. (defun lead-to-gold (x)
  1957.   "Takes a quantity of lead and returns gold."
  1958.   (when (> (phlogiston:heat-flow 5 x x)  ;Using a qualified symbol
  1959.            3)
  1960.     (make-fire-bottle x))                ;Using an imported symbol
  1961.   (gild x))
  1962.  
  1963. ;;; And so on ...
  1964.  
  1965. ----------------------------------------------------------------
  1966.  
  1967. The file phlogiston-package is shown in table 11-7. This one is a little more
  1968. straightforward than the file alchemy-package, because the latter bears the
  1969. responsibility for breaking the circular package references. This file simply
  1970. makes sure that the alchemy package is defined and then performs a defpackage
  1971. for the phlogiston package.
  1972.  
  1973.  
  1974.  
  1975. ----------------------------------------------------------------
  1976. Table 11-7: File phlogiston-package Using defpackage
  1977.  
  1978. ;;;; Phlogiston package definition, by Thermofluidics, Ltd.
  1979.  
  1980. ;;; This package uses functions from the ALCHEMY package.
  1981.  
  1982. (cl:unless (cl:find-package "ALCHEMY")
  1983.   (cl:load "alchemy-package"))
  1984.  
  1985. (cl:defpackage "PHLOGISTON"
  1986.   (:use "COMMON-LISP" "ALCHEMY")
  1987.   (:export "HEAT-FLOW"
  1988.            "COLD-FLOW"
  1989.            "MIX-FLUIDS"
  1990.            "SEPARATE-FLUIDS"
  1991.            "BURN"
  1992.            "MAKE-FIRE-BOTTLE")
  1993.   )
  1994.  
  1995. ----------------------------------------------------------------
  1996.  
  1997. The phlogiston file, shown in table 11-8, simply loads the phlogiston package
  1998. definition, makes that package current, and then defines the ``real contents''
  1999. of the package.
  2000.  
  2001.  
  2002.  
  2003. ----------------------------------------------------------------
  2004. Table 11-8: File phlogiston Using defpackage
  2005.  
  2006. ;;;; Phlogiston functions, by Thermofluidics, Ltd.
  2007.  
  2008. (unless (find-package "PHLOGISTON")
  2009.   (load "phlogiston-package"))
  2010.  
  2011. (in-package 'phlogiston)
  2012.  
  2013. (defvar *feeling-weak* nil)
  2014.  
  2015. (defun heat-flow (amount x y)
  2016.   "Make some amount of heat flow from x to y."
  2017.   (when *feeling-weak*
  2018.     (quaff (elixir-of-life)))     ;No qualifier is needed.
  2019.   (push-heat amount x y))
  2020.  
  2021. ;;; And so on ...
  2022.  
  2023. ----------------------------------------------------------------
  2024.  
  2025. Let's look at the question of package circularity in this example a little more
  2026. closely. Suppose that the file alchemy-package is loaded first. It defines the
  2027. alchemy package and then loads file phlogiston-package. That file in turn finds
  2028. that the package alchemy has already been defined and therefore does not
  2029. attempt to load file alchemy-package again; it merely defines package
  2030. phlogiston. The file alchemy-package then has a chance to import
  2031. phlogiston:make-fire-bottle and everything is fine.
  2032.  
  2033. On the other hand, suppose that the file phlogiston-package is loaded first. It
  2034. finds that the package alchemy has not already been defined, and therefore it
  2035. immediately loads file alchemy-package. That file in turn defines the alchemy
  2036. package; then it finds that package phlogiston is not yet defined and so loads
  2037. file phlogiston-package again (indeed, in nested fashion). This time file
  2038. phlogiston-package does find that the package alchemy has already been defined,
  2039. so it simply defines package phlogiston and terminates. The file
  2040. alchemy-package then imports phlogiston:make-fire-bottle and terminates.
  2041. Finally, the outer loading of file phlogiston-package re-defines package
  2042. phlogiston. Oh, dear. Fortunately the two definitions of package phlogiston
  2043. agree in every detail, so everything ought to be all right. Still, it looks a
  2044. bit dicey; I certainly don't have the same warm, fuzzy feeling that I would if
  2045. no package were defined more than once.
  2046.  
  2047. Conclusion: defpackage goes a long way, but it certainly doesn't solve all the
  2048. possible problems of package and file management. Neither did require and
  2049. provide. Perhaps further experimentation will yield facilities appropriate for
  2050. future standardization.
  2051. [change_end]
  2052.  
  2053. -------------------------------------------------------------------------------
  2054.  
  2055.  
  2056.  
  2057.  
  2058.